<?php
namespace App\Http\Controllers\Business\Shop\V1\Salesman;

use App\Models\Shop;
use App\Models\ShopUser;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use App\Models\ShopSalesmanWithdrawal;
use App\Utils\EncryptTool;


class SalesmanManagement {
    //地推管理首页
    public static function index($shopUserId, $shopId){
        if(!$shopUserId || !$shopId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //加密， rawurlencode;
        //$shopUserId = EncryptTool::encrypt($shopUserId, 'E', '');
        //$shopUserId = rawurlencode($shopUserId);

        //$qrCodeUrl = env('WEB_URL') . "mobile/salesman/gotodownApp?shopid=" . $shopId . "&shopuserid=" . $shopUserId;
        $qrCodeUrl = env('WEB_URL') . "qrcode/shop/" . $shopId . "?shopuserid=" . $shopUserId;
        $isChild = 1;
        $isPartnerSalesman = 0;
        $shopParentMobile = Shop::where('id', $shopId)->pluck('parentmobile')->first();
        $shopUserMobile = ShopUser::where('id', $shopUserId)->pluck('mobile')->first();
        if(ShopSalesman::where('shopid', $shopId)->pluck('type')->first() == 'partner') {
            $isPartnerSalesman = 1;
        }
        if($shopParentMobile == $shopUserMobile) {
            $isChild = 0;
        }
        $instruction = '有效地推：同城新用户下载翻贝网App，首次登录时扫描地推二维码，获取邀请码完成登录即可。首次登录时未获取邀请码，可在2小时内用扫一扫功能扫地推二维码即可。

注：非同城用户或同一设备登录的新用户会被判定为无效。';
        $data = [
            'qrcodeurl' => $qrCodeUrl,
            'ischild' => $isChild,
            'ispartnersalesman' => $isPartnerSalesman,
            'instruction'=>$instruction,
        ];
        return ['status'=>'success', 'data'=>$data];
    }

    //地推注册列表
    public static function registerList($shopId, $shopUserId, $source, $page, $startTime='', $lastTime='', $perpage=20) {
        if(!$shopId || !$shopUserId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            $startTime = "2016-07-01 00:00:00";
            $lastTime = date("Y-m-d H:i:s");
        } else {
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        }
        $skip = ($page - 1 ) * $perpage;
        $shopUserAuth = ShopUser::where('id', $shopUserId)->pluck('auth')->first();
        if(!$shopUserAuth) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopUserAuth = json_decode($shopUserAuth, true);
        if(!isset($shopUserAuth[$shopId])) {
            return ['status'=>'failed', 'message'=>'无权访问'];
        }
        $shopMobile = Shop::where('id', $shopId)->select('parentmobile','childmobile')->first();
        $childArray = [
            [ "id"=>"-2", "name"=> "所有地推" ],
            [ "id"=>"-1", "name"=> "所有子账号" ],
            [ "id"=>"0", "name"=> "默认" ],
        ];
        if($shopMobile) {
            $childMobile = $shopMobile['childmobile'];
            $parentMobile = $shopMobile['parentmobile'];
            $childMobileArray = json_decode($childMobile, true);
            if(is_array($childMobileArray) ) {
                if(!in_array($parentMobile, $childMobileArray)) {
                    array_push($childMobileArray, $parentMobile);
                }
            } else {
                $childMobileArray = [$parentMobile];
            }
            $shopUserNameArray = ShopUser::whereIn('mobile', $childMobileArray)->orderBy('id', 'asc')->select('id','name')->get();
            foreach($shopUserNameArray as $list) {
                if($list['id'] == $shopUserId) {
                    array_push($childArray, ["id"=>(string)$list['id'], "name"=>"本人"]);
                } else {
                    array_push($childArray, ["id"=>(string)$list['id'], "name"=>$list['name']]);
                }
            }
        }
        if($source == '-2') {
            $lists = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('type', 'register')
                ->select('created_at', 'mobile', 'registersource', 'shopuserid', 'getmoney')
                ->skip($skip)->take($perpage)->get();
            $validCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('type', 'register')
                ->where('getmoney', '<>', 0)
                ->count();
            $invalidCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('type', 'register')
                ->where('getmoney', 0.00)
                ->count();
        } elseif($source == '-1') {
            $lists = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('registersource', '!=', '默认')
                ->where('type', 'register')
                ->select('created_at', 'mobile', 'registersource', 'shopuserid', 'getmoney')
                ->skip($skip)->take($perpage)->get();
            $validCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('registersource', '!=', '默认')
                ->where('type', 'register')
                ->where('getmoney', '<>', 0)
                ->count();
            $invalidCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('registersource', '!=', '默认')
                ->where('type', 'register')
                ->where('getmoney', 0.00)
                ->count();
        } elseif($source == '0') {
            $lists = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('registersource', '默认')
                ->where('type', 'register')
                ->select('created_at', 'mobile', 'registersource', 'shopuserid', 'getmoney')
                ->skip($skip)->take($perpage)->get();
            $validCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('registersource', '默认')
                ->where('type', 'register')
                ->where('getmoney', '<>', 0)
                ->count();
            $invalidCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('registersource', '默认')
                ->where('type', 'register')
                ->where('getmoney', 0.00)
                ->count();
        } else {
            $lists = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('shopuserid', $source)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('type', 'register')
                ->select('created_at', 'mobile', 'registersource', 'shopuserid', 'getmoney')
                ->skip($skip)->take($perpage)->get();
            $validCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('shopuserid', $source)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('type', 'register')
                ->where('getmoney', '<>', 0)
                ->count();
            $invalidCount = ShopSalesmanDetail::where('shopid', $shopId)
                ->where('shopuserid', $source)
                ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->where('type', 'register')
                ->where('getmoney', 0.00)
                ->count();
        }
        $newLists = [];
        foreach($lists as $list) {
            $newList['created_at'] = date("Y-m-d H:i:s", strtotime($list['created_at']));
            $newList['mobile'] = str_replace(substr($list['mobile'], 3, 4), '****', $list['mobile']);
            $newList['registersource'] = $list['registersource'];
            $newList['shopuserid'] = $list['shopuserid'];
            if($list['shopuserid'] != '') {
                if($list['shopuserid'] == $shopUserId) {
                    $newList['registersource'] = '本人';
                }
            }
            $newList['getmoney'] = $list['getmoney'];
            array_push($newLists, $newList);
        }
        $data = [
            'lists' => $newLists,
            'validcount' => $validCount,
            'invalidcount' => $invalidCount,
            'select' => $childArray
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //地推订单列表
    public static function validOrderList($shopId, $page, $startTime='', $lastTime='', $perpage=20) {
        if(!$shopId ) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            $startTime = "2016-07-01 00:00:00";
            $lastTime = date("Y-m-d H:i:s");
        } else {
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        }
        $skip = ($page-1) * $perpage;
        $lists = ShopSalesmanDetail::where('shopid', $shopId)
            ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
            ->where('type', 'order')
            ->select('ordertime','orderamount', 'mobile')
            ->skip($skip)->take($perpage)->get();
        foreach($lists as $k=>$val) {
            $lists[$k]['mobile'] = str_replace(substr($lists[$k]['mobile'], 3, 4), '****', $lists[$k]['mobile']);
        }
        return ['status'=>'success', 'data'=>$lists];
    }
    //地推订单列表 v2版本
    public static function validOrderListByV2Version($shopId, $page, $startTime='', $lastTime='', $perpage=20) {
        if(!$shopId ) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            $startTime = "2016-07-01 00:00:00";
            $lastTime = date("Y-m-d H:i:s");
        } else {
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        }
        $skip = ($page-1) * $perpage;
        $count = ShopSalesmanDetail::where('shopid', $shopId)
            ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
            ->where('type', 'order')->count();
        $lists = ShopSalesmanDetail::where('shopid', $shopId)
            ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
            ->where('type', 'order')
            ->select('ordertime','orderamount', 'mobile')
            ->skip($skip)->take($perpage)->get();
        foreach($lists as $k=>$val) {
            $lists[$k]['mobile'] = str_replace(substr($lists[$k]['mobile'], 3, 4), '****', $lists[$k]['mobile']);
        }
        $data = [
            'lists' => $lists,
            'count' => $count,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}